//
//  HcCustomKeyboard.m
//  Custom
//
//  Created by 黄诚 on 14/12/18.
//  Copyright (c) 2014年 huangcheng. All rights reserved.
//

#import "HcCustomKeyboard.h"
#import <UIKit/UIKit.h>

//屏幕宽度
#define WIDTH_SCREEN ([[UIScreen mainScreen] bounds].size.width)
//屏幕高度
#define HEIGHT_SCREEN ([[UIScreen mainScreen] bounds].size.height)


@implementation HcCustomKeyboard
@synthesize mDelegate;

static HcCustomKeyboard *customKeyboard = nil;

+(HcCustomKeyboard *)customKeyboard
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [[HcCustomKeyboard alloc] init];
        }
        return customKeyboard;
    }
}
+(id)allocWithZone:(struct _NSZone *)zone //确保使用者alloc时 返回的对象也是实例本身
{
    @synchronized(self)
    {
        if (customKeyboard == nil)
        {
            customKeyboard = [super allocWithZone:zone];
        }
        return customKeyboard;
    }
}
+(id)copyWithZone:(struct _NSZone *)zone //确保使用者copy时 返回的对象也是实例本身
{
    return customKeyboard;
}

-(void)textViewShowView:(UIViewController *)viewController customKeyboardDelegate:(id)delegate andType:(HcCustomKeyboardType)type andplaceholder:(NSString *)placeholder
{
    self.mViewController =viewController;
    self.mDelegate =delegate;
    self.isTop = NO;//初始化的时候设为NO
    

    CGFloat msecondaryBackViewH = self.mSecondaryBackView.frame.size.height;
    CGFloat msecondaryBackViewY =CGRectGetMaxY(self.mSecondaryBackView.frame);
    
    self.mBackView =[[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT_SCREEN+140, WIDTH_SCREEN, 160)];
    NSLog(@"%p",self.mBackView);
    self.mBackView.backgroundColor =[UIColor colorWithRed:244/255.0 green:245/255.0 blue:247/255.0 alpha:1];
    [self.mViewController.view addSubview:self.mBackView];
    self.mSecondaryBackView =[[UIView alloc]initWithFrame:CGRectMake(10, 10,WIDTH_SCREEN-20,100)];

    [self.mBackView addSubview:self.mSecondaryBackView];
    
    self.mTextView =[[RPFloatingPlaceholderTextView alloc]initWithFrame:CGRectMake(0,0, WIDTH_SCREEN-20,100)];
    self.mTextView.backgroundColor =[UIColor whiteColor];
    self.mTextView.delegate = self;
    self.mTextView.placeholder = placeholder;
    self.mTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.mTextView.layer.cornerRadius = 5;
    self.mTextView.layer.borderWidth = 1;
    self.mTextView.layer.borderColor = [UIColor colorWithRed:210.0/255.0 green:210.0/255.0 blue:210.0/255.0 alpha:1].CGColor;
    [self.mSecondaryBackView addSubview:self.mTextView];
    
    self.mTalkBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.mTalkBtn.frame = CGRectMake(WIDTH_SCREEN-60, (160-msecondaryBackViewH-10-30)/2 +msecondaryBackViewY,50, 30);
    [self.mTalkBtn setTitle:@"发布" forState:UIControlStateNormal];
    [self.mTalkBtn addTarget:self action:@selector(forTalk) forControlEvents:UIControlEventTouchUpInside];
    [self.mTalkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
   self.mTalkBtn.backgroundColor = [UIColor colorWithRed:202/255.0 green:202/255.0 blue:202/255.0 alpha:1];
    self.mTalkBtn.layer.cornerRadius = 5;
    [self.mBackView addSubview:self.mTalkBtn];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
    
    if (type == HcCustomKeyboardTypeBtton) {
        [self.mTextView becomeFirstResponder];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.mTextView.text = nil;
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSMutableString * changedString=[[NSMutableString alloc]initWithString:textView.text];
    [changedString replaceCharactersInRange:range withString:text];
    
    if (changedString.length!=0) {//不为空
        self.mTalkBtn.enabled=YES;
        self.mTalkBtn.backgroundColor = MainColor;
    }else{
        self.mTalkBtn.enabled=NO;
        self.mTalkBtn.backgroundColor = [UIColor colorWithRed:202/255.0 green:202/255.0 blue:202/255.0 alpha:1];
    }
    return YES;
}

-(void)forTalk //评论按钮
{
    if (self.isTop ==NO)
    {
        [self.mTextView becomeFirstResponder];
    }
    else
    {
        if (self.mTextView.text.length==0)
        {
            NSLog(@"内容为空");
            self.mTalkBtn.enabled=NO;
            return;
        }
        else
        {
            self.mTalkBtn.enabled=YES;
            [self.mTextView resignFirstResponder];
        }
        [mDelegate talkBtnClick:self.mTextView];
    }
}
-(void)hideView //点击屏幕其他地方 键盘消失
{
    NSLog(@"屏幕消失");
    [self.mTextView resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification*)notification //键盘出现
{
    [self.mTextView becomeFirstResponder];
    self.isTop =YES;
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"%f-%f",_keyboardRect.origin.y,_keyboardRect.size.height);
    
    if (!self.mHiddeView)
    {
        self.mHiddeView =[[UIView alloc]initWithFrame:CGRectMake(0, 0,WIDTH_SCREEN,HEIGHT_SCREEN)];
        self.mHiddeView.backgroundColor =[UIColor blackColor];
        self.mHiddeView.alpha =0.0f;
        [self.mViewController.view addSubview:self.mHiddeView];
        
        UIButton *hideBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        hideBtn.backgroundColor =[UIColor clearColor];
        hideBtn.frame = self.mHiddeView.frame;
        [hideBtn addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
        [self.mHiddeView addSubview:hideBtn];
    }
    
    [UIView animateWithDuration:0.1 animations:^{
//        NSLog(@"11222 = %f = %f",HEIGHT_SCREEN,_keyboardRect.size.height);
        self.mHiddeView.alpha =0.4f;
        self.mBackView.frame =CGRectMake(0, HEIGHT_SCREEN-_keyboardRect.size.height-160, WIDTH_SCREEN, 160);
        self.mSecondaryBackView.frame =CGRectMake(10, 10,WIDTH_SCREEN-20,100);
        CGFloat msecondaryBackViewH = self.mSecondaryBackView.frame.size.height;
        CGFloat msecondaryBackViewY =CGRectGetMaxY(self.mSecondaryBackView.frame);
        self.mTalkBtn.frame =CGRectMake(WIDTH_SCREEN-60, (160-msecondaryBackViewH-10-30)/2 +msecondaryBackViewY, 50, 30);
        [self.mViewController.view bringSubviewToFront:self.mBackView];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)keyboardWillHide:(NSNotification*)notification //键盘下落
{    
    self.isTop =NO;
        [UIView animateWithDuration:0.3 animations:^{
        self.mBackView.frame=CGRectMake(0, HEIGHT_SCREEN+20
, WIDTH_SCREEN, 50);
        self.mHiddeView.alpha =0.0f;
        self.mSecondaryBackView.frame =CGRectMake(10, 10,WIDTH_SCREEN-20,28);
    } completion:^(BOOL finished) {
        [self.mHiddeView removeFromSuperview];
        self.mHiddeView =nil;

    }];
}
- (void)textDidChanged:(NSNotification *)notif //监听文字改变 换行时要更改输入框的位置
{
//    CGSize contentSize = self.mTextView.contentSize;
//    
//    if (contentSize.height > 140){
//        return;
//    }
//    CGFloat minus = 3;
//    CGRect selfFrame = self.mBackView.frame;
//    CGFloat selfHeight = self.mTextView.superview.frame.origin.y * 2 + contentSize.height - minus + 2 * 2;
//    CGFloat selfOriginY = selfFrame.origin.y - (selfHeight - selfFrame.size.height);
//    selfFrame.origin.y = selfOriginY;
//    selfFrame.size.height = selfHeight;
//    self.mBackView.frame = selfFrame;
//    self.mSecondaryBackView.frame =CGRectMake(10, 10, 230, selfHeight-20);
//    NSLog(@"文字改变");
}

-(void)dealloc //移除通知
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

@end

